<script setup>
import { ElMessageBox } from "element-plus";
import { Menu as IconMenu, Location } from "@element-plus/icons-vue";
import useUserStore from "@/stores/user";
import { useRouter } from "vue-router";
import { getMenusApi } from "@/api/menu";
import { ref } from "vue";
const userStore = useUserStore();
const router = useRouter();
// 退出
const clickLogout = async () => {
  // 1.删除或退出这些操作最好使用确认框，让用户有反悔机会
  const res = await ElMessageBox.confirm("确定要退出登录吗?", "退出登录", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).catch((error) => error);

  if (res === "cancel") return;

  // 2.删除token与用户信息
  userStore.updateToken(null);
  userStore.delUserInfo();
  // 3.跳转到登录页
  router.push("/login");
};

// 获取用户数据
userStore.getUserInfo();

// 获取侧边栏菜单
const menus = ref([]);
const getMenus = async () => {
  const { data: res } = await getMenusApi();
  menus.value = res.data;
};
getMenus();
</script>

<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="@/assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <div class="userInfo">
        <img
          :src="userStore.userInfo.user_pic"
          alt=""
          v-if="userStore.userInfo.user_pic"
          class="avatar"
        />
        <img src="@/assets/images/default.png" alt="" v-else class="avatar" />
        <span>{{
          userStore.userInfo.nickname || userStore.userInfo.username
        }}</span>
        <span @click="clickLogout">退出</span>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <!-- 用户信息 -->
        <div class="user-box">
          <img
            :src="userStore.userInfo.user_pic"
            alt=""
            v-if="userStore.userInfo.user_pic"
          />
          <img src="@/assets/images/default.png" alt="" v-else />
          <span
            >欢迎
            {{
              userStore.userInfo.nickname || userStore.userInfo.username
            }}</span
          >
        </div>
        <!-- 菜单项 -->
        <el-menu
          active-text-color="#f50"
          background-color="#23262e"
          :default-active="'/home'"
          text-color="#fff"
          unique-opened
          router
        >
          <template v-for="(item, index) in menus" :key="index">
            <!-- 有子菜单 -->
            <el-sub-menu :index="item.indexPath" v-if="item.children">
              <template #title>
                <el-icon><location /></el-icon>
                <span>{{ item.title }}</span>
              </template>

              <el-menu-item
                :index="subItem.indexPath"
                v-for="subItem in item.children"
              >
                {{ subItem.title }}
              </el-menu-item>
            </el-sub-menu>

            <!-- 没有子菜单 -->
            <el-menu-item :index="item.indexPath" v-else>
              <el-icon><icon-menu /></el-icon>
              <span>{{ item.title }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <!-- 二级路由容器 -->
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© CMS系统</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }

  .el-aside {
    .user-box {
      height: 70px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-top: 1px solid #000;
      border-bottom: 1px solid #000;
      user-select: none;
      img {
        width: 35px;
        height: 35px;
        border-radius: 50%;
        background-color: #fff;
        margin-right: 15px;
        object-fit: cover;
      }
      span {
        color: white;
        font-size: 12px;
      }
    }
    .el-menu {
      border: none;
    }
  }

  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
    .userInfo {
      display: flex;
      align-items: center;
      span {
        margin: 0 5px;
        color: white;
        font-size: 16px;
      }
    }
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
</style>
